<?php
/**
 * User: hjd-yao
 * Date: 2019/5/8
 * Time: 9:16
 */

namespace DC\ServiceCenter\Http\Controllers\Upload;


use App\Helpers\Uploader\UploadImage;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use OSS\OssClient;
use OSS\Tests\OssClientSignatureTest;

class UploadImgController extends Controller
{
    /**
     * @title 图片上传
     * @param Request $request
     * @return JsonResponse
     */
    public function server(Request $request)
    {

        $config = config('UEditorUpload.upload');
        $upConfig = array(
            "pathFormat" => $config['imagePathFormat'],
            "maxSize" => $config['imageMaxSize'],
            "allowFiles" => $config['imageAllowFiles'],
            'fieldName' => $request->input('fileInputName',$config['imageFieldName']),
            'imageGroup'=>$request->input('image_group','image')
        );
        $result = with(new UploadImage($upConfig, $request))->upload();
        return response()->json($result, 200, [], JSON_UNESCAPED_UNICODE);
    }

    public function ossPolicy()
    {
        $policyText = [
            "expiration"=> "2020-01-01T12:00:00.000Z",
            "conditions"=> [
                    ["content-length-range", 0, 1048576000]
                ]
            ];
        $policyBase64 = base64_encode(json_encode($policyText));

        $signature = base64_encode(hash_hmac('sha1',$policyBase64,config('filesystems.disks.oss.secret_key'),true));

        return response()->json([
            'status'=>1,
            "success"=>true,
            "msg"=>"签名成功",
            "data"=>[
                "host"=>config('filesystems.disks.oss.endpoint'),
                "policy"=>$policyBase64,
                "expire"=>time()+30,
                'accessid'=>config('filesystems.disks.oss.access_key'),
                'signature'=>$signature,
            ]
        ]);
    }
}